package com.zj.filedemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main2Activity extends AppCompatActivity {
    private static final String TAG = Main2Activity.class.getSimpleName();

    private TextView textView;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        textView = (TextView) findViewById(R.id.tv_string);
        imageView = (ImageView) findViewById(R.id.iv_img);

        String str = "Hello World";

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        writeStringToFile(str);
        writeImageToFile(bitmap);

        textView.setText(readStringFromFile());
        imageView.setImageBitmap(readBitmapFromFile());
    }

    private void writeStringToFile(String str) {
        if (!isExternalStorageWritable()) {
            return;
        }

        File dir = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        Log.e(TAG, "writeStringToFile: dir = " + dir.getAbsolutePath());

        if (!dir.exists()) {
            dir.mkdirs();
        }

        File file = new File(dir, "str.txt");
        if (file.exists()) {
            file.delete();
        }

        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            file.createNewFile();

            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);

            bos.write(str.getBytes());

            bos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void writeImageToFile(Bitmap bitmap) {
        if (!isExternalStorageWritable()) {
            return;
        }

        File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        Log.e(TAG, "writeImageToFile: dir = " + dir.getAbsolutePath());

        if (!dir.exists()) {
            dir.mkdirs();
        }

        File file = new File(dir, "ic.png");
        if (file.exists()) {
            file.delete();
        }

        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            file.createNewFile();

            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);

            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();

            bos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String readStringFromFile() {
        if (!isExternalStorageWritable()) {
            return null;
        }

        File dir = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        Log.e(TAG, "readStringFromFile: dir = " + dir.getAbsolutePath());

        if (!dir.exists()) {
            return null;
        }

        File file = new File(dir, "str.txt");
        if (!file.exists()) {
            return null;
        }

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        StringBuffer sb = new StringBuffer();
        try {
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);

            String line;
            sb.append(br.readLine());
            while ((line = br.readLine()) != null) {
                sb.append("\n" + line);
            }

            br.close();
            isr.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }

    private Bitmap readBitmapFromFile() {
        if (!isExternalStorageWritable()) {
            return null;
        }

        File dir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        Log.e(TAG, "readBitmapFromFile: dir = " + dir.getAbsolutePath());

        if (!dir.exists()) {
            return null;
        }

        File file = new File(dir, "ic.png");
        if (!file.exists()) {
            return null;
        }

        FileInputStream fis = null;
        BufferedInputStream bis = null;

        Bitmap bitmap = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);

            bitmap = BitmapFactory.decodeStream(bis);

            bis.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return bitmap;
    }

    /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        Log.e(TAG, "isExternalStorageWritable: " + state);
        return false;
    }
}
